/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.api.domain.GoodInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.zhengjie.annotation.Log;

import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import me.zhengjie.modules.system.domain.PokerGoodInfo;
import me.zhengjie.modules.system.domain.dto.PokerGoodInfoQueryCriteria;
import me.zhengjie.modules.system.service.PokerGoodInfoService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author greazy
* @date 2025-04-07
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "/good")
@RequestMapping("/api/pokerGoodInfo")
public class PokerGoodInfoController {

    private final PokerGoodInfoService pokerGoodInfoService;

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('pokerGoodInfo:list')")
    public void exportPokerGoodInfo(HttpServletResponse response, PokerGoodInfoQueryCriteria criteria) throws IOException {
        pokerGoodInfoService.download(pokerGoodInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询/good")
    @PreAuthorize("@el.check('pokerGoodInfo:list')")
    public ResponseEntity<PageResult<PokerGoodInfo>> queryPokerGoodInfo(PokerGoodInfoQueryCriteria criteria){
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(pokerGoodInfoService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增/good")
    @ApiOperation("新增/good")
    @PreAuthorize("@el.check('pokerGoodInfo:add')")
    public ResponseEntity<Object> createPokerGoodInfo(@Validated @RequestBody PokerGoodInfo resources){
        pokerGoodInfoService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改/good")
    @ApiOperation("修改/good")
    @PreAuthorize("@el.check('pokerGoodInfo:edit')")
    public ResponseEntity<Object> updatePokerGoodInfo(@Validated @RequestBody PokerGoodInfo resources){
        pokerGoodInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除/good")
    @ApiOperation("删除/good")
    @PreAuthorize("@el.check('pokerGoodInfo:del')")
    public ResponseEntity<Object> deletePokerGoodInfo(@ApiParam(value = "传ID数组[]") @RequestBody List<Integer> ids) {
        pokerGoodInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping("/listGood")
    public ResponseEntity<List<PokerGoodInfo>> listGood( ){
        LambdaQueryWrapper<PokerGoodInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PokerGoodInfo::getIsHas,1);
        return new ResponseEntity<>(pokerGoodInfoService.list(wrapper),HttpStatus.OK);
    }

    @GetMapping("/listAll")
    public ResponseEntity<List<PokerGoodInfo>> listAll( ){
        return new ResponseEntity<>(pokerGoodInfoService.list(),HttpStatus.OK);
    }


    @GetMapping("/input")
    public ResponseEntity<Object> input( ){
        String filePath = "C:\\Users\\GEZONGYUAN\\Documents\\WeChat Files\\wxid_6ca6jlad39sy22\\FileStorage\\File\\2025-04\\items.json";

        // 读取整个文件内容为字符串
        String content = FileUtil.readString(filePath, "UTF-8");

        // 将字符串转换为JSONArray对象
        JSONArray jsonArray = JSONUtil.parseArray(content);
        JSONObject jsonobject = (JSONObject)jsonArray.get(2);
        JSONArray data = jsonobject.getJSONArray("data");
        ArrayList<PokerGoodInfo> goodInfos = new ArrayList<>();
        // 遍历JSONArray中的每个JSONObject
        for (int i = 0; i < data.size(); i++) {
            JSONObject jsonObject = data.getJSONObject(i);
            String itemKey = jsonObject.getStr("item_key");
            String itemName = jsonObject.getStr("item_name");
            PokerGoodInfo goodInfo = new PokerGoodInfo();
            goodInfo.setGoodId(itemKey);
            goodInfo.setGoodName(itemName);
            goodInfo.setIsHas(0);
            goodInfo.setJettonNum(0);
            goodInfos.add(goodInfo);
        }
        pokerGoodInfoService.saveBatch(goodInfos);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}